package org.anupta.realm.entity.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.anupta.realm.entity.entities.Player;
import org.anupta.realm.utils.others.Config;
import org.anupta.realm.utils.sql.Database;

public class PlayerDAO extends DAO<Player> {

	@Override
	public Player find(int id) {
		Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Player player = null;
        try {
        	connection = Database.getDatabase(Config.LOGIN[0]).getSource().getConnection();
            statement = connection.prepareStatement("SELECT * FROM players WHERE guid = ?;");
            statement.setInt(1, id);
            resultSet = statement.executeQuery();

            if (resultSet.first()) {
            	player = new Player(
                resultSet.getInt("guid"),
                resultSet.getString("name"),
                resultSet.getByte("gameGuid"));
            	}
        } catch (SQLException ex) {
        	Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        	try {
        		resultSet.close();
        		statement.close();
                connection.close();
            } catch (SQLException ex) {
            	Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return player;
	}

	@Override
	public Player find(String name) {
		Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Player player = null;
        try {
        	connection = Database.getDatabase(Config.LOGIN[0]).getSource().getConnection();
            statement = connection.prepareStatement("SELECT * FROM players WHERE name = ?;");
            statement.setString(1, name);
            resultSet = statement.executeQuery();

            if (resultSet.first()) {
            	player = new Player(
            			resultSet.getInt("guid"),
                    	resultSet.getString("name"),
                    	resultSet.getByte("gameGuid"));
            }
        } catch (SQLException ex) {
        	Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        	try {
        		resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException ex) {
            	Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return player;
	}

	@Override
	public List<Player> findAll() {
		return null;
	}

	@Override
	public void commit() {
		for (Player player : toCreate.values()) {
    		Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    		PreparedStatement statement = null;
    		try {
				statement = connection.prepareStatement("INSERT INTO players VALUES(?,?,?);");
				statement.setInt(1, player.getGuid());
				statement.setString(2, player.getName());
				statement.setInt(3, player.getGameGuid());
				statement.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					statement.close();
					connection.close();
					toCreate.remove(player);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
        }

        for (Player player : toUpdate.values()) {
        	Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    		PreparedStatement statement = null;
    		try {
    			statement = connection.prepareStatement("UPDATE players SET " +
    													"name = ?," +
    													"gameGuid = ?" +
    													" WHERE guid = ?;");
				statement.setString(1, player.getName());
				statement.setInt(2, player.getGameGuid());
				statement.setInt(3, player.getGuid());
				statement.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					statement.close();
					connection.close();
					toUpdate.remove(player);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
        }

        for (Player player : toDelete.values()) {
        	Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    		PreparedStatement statement = null;
    		try {
				statement = connection.prepareStatement("DELETE FROM players WHERE guid = ?;");
				statement.setInt(1, player.getGuid());
				statement.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					statement.close();
					connection.close();
					toDelete.remove(player);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
        }
	}
}
